<template>
  <el-container style="padding: 15px">
    <el-header height="130px">
      <el-form size="small" label-width="70px" :model="searchObj">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="不动产类型" label-width="85px">
              <el-select v-model="searchObj.projectId">
                <el-option v-for="item in projectTypeList" :key="item.id" :label="item.projectName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托人">
              <el-input v-model="searchObj.reportClientFullName" />
            </el-form-item>
          </el-col>

          <!--          <el-col :span="4">-->
          <!--            <el-form-item label="查勘人员">-->
          <!--              <el-select>-->
          <!--                <el-option value="" />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->

        </el-row>
        <el-row type="flex" :gutter="20" align="middle">

          <el-col :span="6">
            <el-form-item label="产权人">
              <el-input />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="报告类型">
              <el-select v-model="searchObj.reportTypeId">
                <el-option v-for="item in reportTypeList" :key="item.id" :label="item.reportTypeName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="searchObj.leafId" multiple>
                <el-option v-for="item in reportStatusList" :key="item.id" :label="item.leafName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" :gutter="20" align="middle" justify="center">
          <el-col :span="4">
            <el-form-item label-width="0px">
              <el-button type="primary" @click="getPage">搜索</el-button>
              <el-button @click="resetPage">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-table v-loading="listLoading" :data="archivesList">
        <el-table-column label="项目编号" prop="reportId" width="120px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="不动产类型" prop="projectName" />
        <el-table-column label="项目名称" prop="reportName" width="300px" />
        <el-table-column label="估价员" prop="valuerName" />
        <el-table-column label="审核状态" prop="leafName" />
        <el-table-column label="报告类型" prop="reportTypeName" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="backReport(scope.row)">退回</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { validObj } from '@/utils/validate'
import { archivesRoomList, archivesSearchList, archivesSearchPage } from '@/api/report'
import { ellipsisString } from '@/utils'

export default {
  name: 'Index',
  data() {
    return {
      archivesList: [],
      reportStatusList: [],
      reportTypeList: [],
      projectTypeList: [],

      searchObj: {},

      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      listLoading: false

    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getPage()
      this.getSearchFormList()
    },
    getPage() {
      const searchObj = this.searchObj
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      this.listLoading = true
      if (validObj(searchObj)) {
        // 搜索
        archivesSearchPage(searchObj, currentPage, sizePage).then(res => {
          this.archivesList = res.data.list
          this.totalPage = res.data.count
        })
        this.listLoading = false
      } else {
        archivesRoomList(currentPage, sizePage).then(res => {
          this.archivesList = res.data.list
          this.totalPage = res.data.total
        }).finally(() => {
          this.listLoading = false
        })
      }
    },

    toDetails(id) {
      this.$router.push({ path: '/report/details', query: { id: id }})
    },

    handleSizeChange(val) {
      this.sizePage = val
      this.getPage()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.getPage()
    },

    backReport() {},

    getSearchFormList() {
      archivesSearchList().then(res => {
        console.log(res)
        this.reportStatusList = res.data.cacheList
        this.reportTypeList = res.data.types
        this.projectTypeList = res.data.projects
      })
    },

    resetPage() {
      this.searchObj = {}
      this.currentPage = 1
      this.sizePage = 10
      this.getPage()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },

    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    }
  }
}
</script>

<style scoped>

</style>
